package com.shycloud.mido.member.pattern;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shycloud.mido.common.core.exception.BusinessException;
import com.shycloud.mido.common.core.util.SpringContextHolder;
import com.shycloud.mido.member.api.entity.MultiAssist;
import com.shycloud.mido.member.api.entity.MultiAssistRecord;
import com.shycloud.mido.member.api.util.Constant;
import com.shycloud.mido.member.api.vo.MultiAssistRecordVO;
import com.shycloud.mido.member.service.MultiAssistRecordService;
import com.shycloud.mido.member.service.MultiAssistService;

/**
 * @author baiqiang.
 * @Description: TODO
 * @date 2021年7月14日 上午10:56:27
 * @ClassName: ISubjectBuilder
 */
abstract class StrategyBuilder {

	protected Map<String, ISubjectStrategy> strategyMap = new HashMap<>();

	public abstract StrategyBuilder buildOptAssign();

	public abstract StrategyBuilder buildOrgAssign();

	public abstract StrategyBuilder buildTeacherAssign();

	public abstract StrategyBuilder buildStudentOpening();

	public Map<String, ISubjectStrategy> build() {
		return this.strategyMap;
	}

	protected <T extends SubjectPayload, R extends MultiAssist> void commonStrategy(T payload, Function<T, R> function) {

		MultiAssistRecordVO vo = payload.getMultiAssistRecordVO();
		//获取开通几个月 记录
		Integer monthly = vo.getMonthly();
		//1增加 2减少
		String type = vo.getType();
		//获取天天帮service
		MultiAssistRecordService multiAssistRecordService = SpringContextHolder.getBean(MultiAssistRecordService.class);
		MultiAssistService multiAssistService = SpringContextHolder.getBean(MultiAssistService.class);
		//操作时间
		LocalDateTime now = LocalDateTime.now();
		//发起方处理
		LambdaQueryWrapper<MultiAssist> multiAssistLaunchQW =
				Wrappers.<MultiAssist>lambdaQuery()
						.eq(MultiAssist::getSubject, vo.getSubjectLaunch())
						.eq(MultiAssist::getDelFlag, Constant.DEL_FLAG);

		MultiAssistRecord multiAssistRecordLaunch = new MultiAssistRecord();
		multiAssistRecordLaunch.setSubject(payload.getMultiAssistRecordVO().getSubjectLaunch());
		if (StringUtils.equalsAny(multiAssistRecordLaunch.getSubject(), Constant.ASSIST_SUBJECT_MASTER, Constant.ASSIST_SUBJECT_OPT)) {
			multiAssistLaunchQW.eq(MultiAssist::getOptId, Integer.valueOf(vo.getCommonId()));
			multiAssistRecordLaunch.setOptId(Integer.valueOf(vo.getCommonId()));
		} else if (StringUtils
				.equalsAny(multiAssistRecordLaunch.getSubject(), Constant.ASSIST_SUBJECT_ORG, Constant.ASSIST_SUBJECT_STUDIO)) {
			multiAssistLaunchQW.eq(MultiAssist::getOrgId, vo.getCommonId());
			multiAssistRecordLaunch.setOrgId(Integer.valueOf(vo.getCommonId()));
		} else if (StringUtils.equals(multiAssistRecordLaunch.getSubject(), Constant.ASSIST_SUBJECT_TEACHER)) {
			multiAssistLaunchQW.eq(MultiAssist::getTeacherId, vo.getCommonId());
			multiAssistRecordLaunch.setTeacherId(vo.getCommonId());
		} else if (StringUtils.equals(multiAssistRecordLaunch.getSubject(), Constant.ASSIST_SUBJECT_STUDENT)) {
			//当前无业务 学生无法分配
			multiAssistLaunchQW.eq(MultiAssist::getStudentId, vo.getCommonId());
			multiAssistRecordLaunch.setStudentId(vo.getCommonId());
			throw new BusinessException("学生无法分配课时");
		}
		MultiAssist multiAssistLaunchPO = multiAssistService.getOne(multiAssistLaunchQW);

		//更新天天帮时长 反向处理
		if (Objects.isNull(multiAssistLaunchPO)) {
			throw new BusinessException("当前用户数据不存在");
		} else {
			//更新天天帮时长
			if (StringUtils.equals(Constant.ASSIST_TYPE_PLUS, type)) {
				if (multiAssistLaunchPO.getMonthly() - monthly < 0) {
					throw new BusinessException("时长不足无法处理");
				}
				multiAssistRecordLaunch.setType(Constant.ASSIST_TYPE_MINUS);
				multiAssistLaunchPO.setMonthly(multiAssistLaunchPO.getMonthly() - monthly);
//				multiAssistLaunchPO.setDuration(multiAssistLaunchPO.getMonthly() * 30);
			} else if (StringUtils.equals(Constant.ASSIST_TYPE_MINUS, type)) {
				multiAssistLaunchPO.setMonthly(multiAssistLaunchPO.getMonthly() + monthly);
//				multiAssistLaunchPO.setDuration(multiAssistLaunchPO.getMonthly() * 30);
				multiAssistRecordLaunch.setType(Constant.ASSIST_TYPE_PLUS);
			} else {
				throw new BusinessException("没有分配类型");
			}
			multiAssistLaunchPO.setUpdateTime(now);
			multiAssistLaunchPO.setUpdateUser(String.valueOf(vo.getMemberId()));
			multiAssistService.updateById(multiAssistLaunchPO);
			//外键ID
			multiAssistRecordLaunch.setAssistId(multiAssistLaunchPO.getId());
		}

		//插入天天帮 记录
		multiAssistRecordLaunch.setScope(vo.getScope());
		multiAssistRecordLaunch.setMonthly(monthly);
		multiAssistRecordLaunch.setCreateTime(now);
		multiAssistRecordLaunch.setCreateUser(String.valueOf(vo.getMemberId()));
		multiAssistRecordLaunch.setUpdateTime(now);
		multiAssistRecordLaunch.setUpdateUser(String.valueOf(vo.getMemberId()));
		multiAssistRecordLaunch.setDelFlag(Constant.DEL_FLAG);
		multiAssistRecordLaunch.setFkRecordId("-1");//发起方标记
		multiAssistRecordService.save(multiAssistRecordLaunch);

		//接收方处理
		MultiAssistRecord multiAssistRecord = new MultiAssistRecord();
		MultiAssist multiAssist = new MultiAssist();

		BeanUtils.copyProperties(vo, multiAssistRecord);
		multiAssistRecord.setFkRecordId(multiAssistRecordLaunch.getId());//发起方ID

		BeanUtils.copyProperties(vo, multiAssist);

//		LocalDateTime endTime = now.plusMonths(monthly);
//		long between = ChronoUnit.DAYS.between(now, endTime);
//		multiAssistRecord.setDuration((int)between);

		MultiAssist multiAssistPO = function.apply(payload);
		//无需消费时间 所有没有开始和结束时长 只有月份
		if (Objects.isNull(multiAssistPO)) {
			//插入天天帮
			multiAssist.setCreateTime(now);
			multiAssist.setCreateUser(String.valueOf(vo.getMemberId()));
			multiAssist.setUpdateTime(now);
			multiAssist.setUpdateUser(String.valueOf(vo.getMemberId()));
			multiAssist.setDelFlag(Constant.DEL_FLAG);
			//更新天天帮时长
			if (StringUtils.equals(Constant.ASSIST_TYPE_PLUS, type)) {
				multiAssist.setMonthly(vo.getMonthly());
//				multiAssist.setDuration(vo.getMonthly() * 30);
			} else if (StringUtils.equals(Constant.ASSIST_TYPE_MINUS, type)) {
				throw new BusinessException("时长不足无法处理");
			} else {
				throw new BusinessException("没有分配类型");
			}
			multiAssistService.save(multiAssist);
			//外键ID
			multiAssistRecord.setAssistId(multiAssist.getId());
		} else {
			//更新天天帮时长
			if (StringUtils.equals(Constant.ASSIST_TYPE_PLUS, type)) {
				multiAssistPO.setMonthly(multiAssistPO.getMonthly() + monthly);
			} else if (StringUtils.equals(Constant.ASSIST_TYPE_MINUS, type)) {
				if (multiAssistPO.getMonthly() - monthly < 0) {
					throw new BusinessException("时长不足无法处理");
				}
				multiAssistPO.setMonthly(multiAssistPO.getMonthly() - monthly);
//				multiAssistPO.setDuration(multiAssistPO.getMonthly() * 30);
			} else {
				throw new BusinessException("没有分配类型");
			}
			multiAssistPO.setUpdateTime(now);
			multiAssistPO.setUpdateUser(String.valueOf(vo.getMemberId()));
			multiAssistService.updateById(multiAssistPO);
			//外键ID
			multiAssistRecord.setAssistId(multiAssistPO.getId());
		}

		//插入天天帮 记录
		multiAssistRecord.setCreateTime(now);
		multiAssistRecord.setCreateUser(String.valueOf(vo.getMemberId()));
		multiAssistRecord.setUpdateTime(now);
		multiAssistRecord.setUpdateUser(String.valueOf(vo.getMemberId()));
		multiAssistRecord.setDelFlag(Constant.DEL_FLAG);
		multiAssistRecordService.save(multiAssistRecord);

	}

}
